package test.lyj;

import test.lyj.model.ActivityThread;
import test.lyj.model.AnotherActivityThread;
import test.lyj.tool.Tool;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ThreadBaseActivity extends Activity {
	private TextView threadText;
	//延迟多少次
	private int loadingSecond=10;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.d("lyjtest", "startOnCreate");
		setContentView(R.layout.thread_base_window);
		Log.d("lyjtest", "startWindowSwithe");
		threadText=(TextView) findViewById(R.id.thread_text);
		ActivityThread textShowthread=new ActivityThread(messageListener,loadingSecond);
		textShowthread.start(); 
	}

	OnClickListener logonClicker = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(ThreadBaseActivity.this, ListBaseActivity.class);
			startActivity(intent);
			// TestLyjLunch.this.finish();

		}
	};


	OnClickListener regClicker = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(ThreadBaseActivity.this, SlipBaseActivity.class);
			startActivity(intent);
			// TestLyjLunch.this.finish();

		}
	};



	OnClickListener testClicker = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(ThreadBaseActivity.this, FooterListActivity.class);
			startActivity(intent);
			// TestLyjLunch.this.finish();

		}
	};
	
	Handler messageListener = new Handler() {
		public void handleMessage(Message msg) {
			Bundle b = msg.getData();
			switch (msg.what) {

			case Tool.TIMER_TASK_INFO:
				String v = b.getString(Tool.TIMER_TASK_RESULT);
				// setTitle(title2);
				// titleInfo.setText("正在加载程序...." + title2+"%");
				if (String.valueOf(loadingSecond).equals(v)) {
					Tool.showMsgDialog(ThreadBaseActivity.this, "运行次数"+loadingSecond);
					AnotherActivityThread anotherShowthread=new AnotherActivityThread(messageListener2,loadingSecond);
					anotherShowthread.start();
//					Intent intent = new Intent();
//					intent.setClass(Cardbox.this, Login.class);
//					startActivity(intent);
//					Cardbox.this.finish();

				} else {
					threadText.setText("当前运行次数"+v);
					// Random r = new Random();
					// int secondNum=r.nextInt(9);
					// logonPercent.setText(title2+String.valueOf(secondNum)+"%");
				}
				break;
			case Tool.TIMER_TASK_COMPELETE:
				String jishuv = b.getString(Tool.TIMER_TASK_RESULT);
				threadText.setText("奇数时候:"+jishuv);
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	Handler messageListener2 = new Handler() {
		public void handleMessage(Message msg) {
			Bundle b = msg.getData();
			switch (msg.what) {

			case Tool.TIMER_TASK_INFO:
				String v = b.getString(Tool.TIMER_TASK_RESULT);
				// setTitle(title2);
				// titleInfo.setText("正在加载程序...." + title2+"%");
				if (String.valueOf(loadingSecond).equals(v)) {
					Tool.showMsgDialog(ThreadBaseActivity.this, "第二个线程运行次数"+loadingSecond);
				} else {
					threadText.setText("第二个线程开始运行,次数"+v);
					// Random r = new Random();
					// int secondNum=r.nextInt(9);
					// logonPercent.setText(title2+String.valueOf(secondNum)+"%");
				}
				break;
			}
			super.handleMessage(msg);
		}
	};

}
